package cn.acac.yygh.user.service.impl;

import cn.acac.yygh.dict.DictFeignClient;
import cn.acac.yygh.model.user.Patient;
import cn.acac.yygh.user.mapper.PatientMapper;
import cn.acac.yygh.user.service.PatientService;
import cn.acac.yygh.utils.JwtHelper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PatientServiceImpl extends ServiceImpl<PatientMapper,Patient> implements PatientService{

    @Autowired
    private DictFeignClient dictFeignClient;

    /**
     * 获取所有的就诊人信息
     * @param token
     * @return
     */
    @Override
    public List<Patient> findPatientList(String token) {
        // 取出token中的userId
        Long userId = JwtHelper.getUserId(token);
        // 设置查询条件
        QueryWrapper<Patient> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        List<Patient> patientList = baseMapper.selectList(queryWrapper);
        patientList.stream().forEach(item -> {
            this.changeCertificatesType(item);
        });
        return patientList;
    }

    /**
     * 根据id获取病人（patient）信息
     * @param id
     * @return
     */
    @Override
    public Patient findPatientById(Long id) {
        QueryWrapper<Patient> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        Patient patient = baseMapper.selectOne(queryWrapper);
        this.changeCertificatesType(patient);
        return patient;
    }

    /**
     * 查询用户底下所有的就诊人信息
     * @param queryWrapper1
     * @return
     */
    @Override
    public List<Patient> selectList(QueryWrapper<Patient> queryWrapper1) {
        List<Patient> patientList = baseMapper.selectList(queryWrapper1);
        patientList.stream().forEach(item -> {
            this.changeCertificatesType(item);
        });
        return patientList;
    }

    /**
     * 将证件类型转换为文字
     * @param patient
     */
    private void changeCertificatesType(Patient patient){
        patient.getParam().put("certificatesTypeString",dictFeignClient.findNameByValue(patient.getCertificatesType()));
        patient.getParam().put("provinceString",dictFeignClient.findNameByValue(patient.getProvinceCode()));
        patient.getParam().put("cityString",dictFeignClient.findNameByValue(patient.getCityCode()));
        patient.getParam().put("districtString",dictFeignClient.findNameByValue(patient.getDistrictCode()));
        patient.getParam().put("fullAddress",
                dictFeignClient.findNameByValue(patient.getProvinceCode()) +
                        dictFeignClient.findNameByValue(patient.getCityCode()) +
                        dictFeignClient.findNameByValue(patient.getDistrictCode()));
    }
}
